package bookhive.datastructures.datasource;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

import org.jopac2.jbal.RecordFactory;
import org.jopac2.jbal.RecordInterface;
import org.jopac2.utils.JOpac2Exception;

import net.jxta.document.Element;
import net.jxta.document.StructuredDocument;

public class DataItem implements DataSourceItem, Comparable<DataItem> {

	private static final String ENCODING = "UTF-8";

	public long id;

	public String type;

	public String data;

	public String hash;

	public DataItem(RecordInterface ri) {
		id = ri.getJOpacID();
		type = ri.getTipo();
		data = ri.toString();
		try {
			hash = ri.getHash();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (JOpac2Exception e) {
			e.printStackTrace();
		}
	}

	public DataItem() {
		id = 0;
		type = null;
		data = null;
		hash = null;
	}

	@SuppressWarnings("unchecked")
	public DataItem(Element recordEl) {
		Enumeration recordAttrs = recordEl.getChildren();
		while (recordAttrs.hasMoreElements()) {
			Element attr = (Element) recordAttrs.nextElement();
			Object value = attr.getValue();
			if (value == null)
				continue;
			if (attr.getKey().toString().equals("Id")) {
				id = Long.parseLong(attr.getValue().toString());
			} else if (attr.getKey().toString().equals("Type")) {
				type = attr.getValue().toString();
				try {
					type = URLDecoder.decode(type, ENCODING);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else if (attr.getKey().toString().equals("Data")) {
				data = attr.getValue().toString();
				try {
					data = URLDecoder.decode(data, ENCODING);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		RecordInterface ri = RecordFactory.buildRecord(id, data, type, 0);
		try {
			hash = ri.getHash();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (JOpac2Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	public void addToParentElement(Element parent, StructuredDocument response)
			throws UnsupportedEncodingException {
		Element recordEl = response.createElement("Record");
		parent.appendChild(recordEl);
		Element attrEl = response.createElement("Id", URLEncoder.encode(String
				.valueOf(id), ENCODING));
		recordEl.appendChild(attrEl);
		attrEl = response.createElement("Type", URLEncoder.encode(type,
				ENCODING));
		recordEl.appendChild(attrEl);
		attrEl = response.createElement("Data", URLEncoder.encode(data,
				ENCODING));
		recordEl.appendChild(attrEl);
	}

	public int compareTo(DataItem di2) {
		return data.compareTo(((DataItem) di2).data);
	}

	public String toString() {
		return "Id: " + id + "\nType: " + type + "\nData: " + data;
	}

	public String getHash() {
		return this.hash;
	}

}
